﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

using SoTaiChinh.Library.Entity;
using SoTaiChinh.Library.Common.SqlServer;

namespace SoTaiChinh.Library.Data
{
    public class ViTriBanner
    {
        public ViTriBanner() { }
        Connection db = new Connection();

        /// <summary>
        /// Inserts a record into the DanhMuc table.
        /// <summary>
        /// <param name="Id"></param>
        /// <param name="Ten"></param>
        /// <param name="TrangThai"></param>
        /// <returns></returns>
        public bool Insert(Entity.ViTriBanner entity)
        {
            bool boolInsert = false;
            SqlConnection SqlCnn = db.ReadFromWebConfig();
            SqlDataAdapter SqlAdp = new SqlDataAdapter();
            SqlCommand SqlCmd = new SqlCommand();
            SqlTransaction SqlTrans = SqlCnn.BeginTransaction();
            try
            {
                SqlCmd.Transaction = SqlTrans;
                SqlCmd.Connection = SqlCnn;
                SqlCmd.CommandText = "Proc_ViTriBanner_Insert";
                SqlCmd.CommandType = CommandType.StoredProcedure;
                SqlCmd.Parameters.Add("@TenViTri", SqlDbType.NVarChar, 255).Value = entity.TenViTri;                
                SqlCmd.Parameters.Add("@TrangThai", SqlDbType.Bit).Value = entity.TrangThai;

                SqlAdp.InsertCommand = SqlCmd;
                SqlAdp.InsertCommand.ExecuteNonQuery();
                SqlCmd.Transaction.Commit();
                boolInsert = true;
                SqlCnn.Close();
            }
            catch
            {
                boolInsert = false;
                SqlCmd.Transaction.Rollback();
            }
            finally
            {
                SqlCmd.Parameters.Clear();
                SqlTrans = null;
            }
            if (SqlCmd != null)
                SqlCmd.Dispose();
            if (SqlAdp != null)
                SqlAdp.Dispose();
            return boolInsert;
        }

        /// <summary>
        /// Selects a single record from the DanhMuc table.
        /// <summary>
        /// <returns>DataTable</returns>
        public DataTable Select()
        {
            DataTable dt = new DataTable();
            SqlConnection SqlCnn = db.ReadFromWebConfig();
            SqlDataAdapter SqlAdp = new SqlDataAdapter();
            SqlCommand SqlCmd = new SqlCommand();
            SqlTransaction SqlTrans = SqlCnn.BeginTransaction();
            try
            {
                SqlCmd.Transaction = SqlTrans;
                SqlCmd.Connection = SqlCnn;
                SqlCmd.CommandText = "Proc_ViTriBanner_Select";
                SqlCmd.CommandType = CommandType.StoredProcedure;
                SqlAdp.SelectCommand = SqlCmd;
                SqlAdp.SelectCommand.ExecuteNonQuery();
                SqlAdp.Fill(dt);
                SqlCmd.Transaction.Commit();
                SqlCnn.Close();
            }
            catch
            {
                SqlCmd.Transaction.Rollback();
            }
            finally
            {
                SqlCmd.Parameters.Clear();
                SqlTrans = null;
            }
            if (SqlCmd != null)
                SqlCmd.Dispose();
            if (SqlAdp != null)
                SqlAdp.Dispose();
            return dt;
        }

        /// <summary>
        /// Selects a single record from the DanhMuc table.
        /// <summary>
        /// <returns>DataTable</returns>
        public DataTable SelectPrimaryKey(Entity.ViTriBanner entity)
        {
            DataTable dt = new DataTable();
            SqlConnection SqlCnn = db.ReadFromWebConfig();
            SqlDataAdapter SqlAdp = new SqlDataAdapter();
            SqlCommand SqlCmd = new SqlCommand();
            SqlTransaction SqlTrans = SqlCnn.BeginTransaction();
            try
            {
                SqlCmd.Transaction = SqlTrans;
                SqlCmd.Connection = SqlCnn;
                SqlCmd.CommandText = "Proc_ViTriBanner_SelectPrimaryKey";
                SqlCmd.CommandType = CommandType.StoredProcedure;
                SqlCmd.Parameters.Add("@Id", SqlDbType.Int).Value = entity.Id;

                SqlAdp.SelectCommand = SqlCmd;
                SqlAdp.SelectCommand.ExecuteNonQuery();
                SqlAdp.Fill(dt);
                SqlCmd.Transaction.Commit();
                SqlCnn.Close();
            }
            catch
            {
                SqlCmd.Transaction.Rollback();
            }
            finally
            {
                SqlCmd.Parameters.Clear();
                SqlTrans = null;
            }
            if (SqlCmd != null)
                SqlCmd.Dispose();
            if (SqlAdp != null)
                SqlAdp.Dispose();
            return dt;
        }


        //Hàm select tất cả giá trị trong bản banner sử dụng ngoài templates
        public DataTable SelectByTenViTriBannerTemplates(Entity.ViTriBanner entity)
        {
            DataTable dt = new DataTable();
            SqlConnection SqlCnn = db.ReadFromWebConfig();
            SqlDataAdapter SqlAdp = new SqlDataAdapter();
            SqlCommand SqlCmd = new SqlCommand();
            SqlTransaction SqlTrans = SqlCnn.BeginTransaction();
            try
            {
                SqlCmd.Transaction = SqlTrans;
                SqlCmd.Connection = SqlCnn;
                SqlCmd.CommandText = "Proc_Banner_SelectByTenViTriBannerTemplates";
                SqlCmd.CommandType = CommandType.StoredProcedure;
                SqlCmd.Parameters.Add("@TenViTri", SqlDbType.NVarChar, 50).Value = entity.TenViTri;

                SqlAdp.SelectCommand = SqlCmd;
                SqlAdp.SelectCommand.ExecuteNonQuery();
                SqlAdp.Fill(dt);
                SqlCmd.Transaction.Commit();
                SqlCnn.Close();
            }
            catch
            {
                SqlCmd.Transaction.Rollback();
            }
            finally
            {
                SqlCmd.Parameters.Clear();
                SqlTrans = null;
            }
            if (SqlCmd != null)
                SqlCmd.Dispose();
            if (SqlAdp != null)
                SqlAdp.Dispose();
            return dt;
        }

        /// Updates a record in the DanhMuc table.
        /// <summary>
        /// <param name="Id"></param>
        /// <param name="DanhMucCha"></param>
        /// <param name="Ten"></param>
        /// <param name="SapXep"></param>
        /// <param name="Tren"></param>
        /// <param name="Trai"></param>
        /// <param name="Phai"></param>
        /// <param name="Duoi"></param>
        /// <param name="Cap"></param>
        /// <param name="TrangThai"></param>
        public bool Update(Entity.ViTriBanner entity)
        {
            bool boolUpdate = false;
            SqlConnection SqlCnn = db.ReadFromWebConfig();
            SqlDataAdapter SqlAdp = new SqlDataAdapter();
            SqlCommand SqlCmd = new SqlCommand();
            SqlTransaction SqlTrans = SqlCnn.BeginTransaction();
            try
            {
                SqlCmd.Transaction = SqlTrans;
                SqlCmd.Connection = SqlCnn;
                SqlCmd.CommandText = "Proc_ViTriBanner_Update";
                SqlCmd.CommandType = CommandType.StoredProcedure;
                SqlCmd.Parameters.Add("@Id", SqlDbType.Int).Value = entity.Id;                
                SqlCmd.Parameters.Add("@TenViTri", SqlDbType.NVarChar, 255).Value = entity.TenViTri;                
                SqlCmd.Parameters.Add("@TrangThai", SqlDbType.Bit).Value = entity.TrangThai;

                SqlAdp.UpdateCommand = SqlCmd;
                SqlAdp.UpdateCommand.ExecuteNonQuery();
                SqlCmd.Transaction.Commit();
                boolUpdate = true;
                SqlCnn.Close();
            }
            catch
            {
                boolUpdate = false;
                SqlCmd.Transaction.Rollback();
            }
            finally
            {
                SqlCmd.Parameters.Clear();
                SqlTrans = null;
            }
            if (SqlCmd != null)
                SqlCmd.Dispose();
            if (SqlAdp != null)
                SqlAdp.Dispose();
            return boolUpdate;
        }

        /// <summary>
        /// Deletes a record from the DanhMuc table by a composite primary key.
        /// <summary>
        public bool Delete(Entity.ViTriBanner entity)
        {
            bool boolDelete = false;
            SqlConnection SqlCnn = db.ReadFromWebConfig();
            SqlDataAdapter SqlAdp = new SqlDataAdapter();
            SqlCommand SqlCmd = new SqlCommand();
            SqlTransaction SqlTrans = SqlCnn.BeginTransaction();
            try
            {
                SqlCmd.Transaction = SqlTrans;
                SqlCmd.Connection = SqlCnn;
                SqlCmd.CommandText = "Proc_ViTriBanner_Delete";
                SqlCmd.CommandType = CommandType.StoredProcedure;
                SqlCmd.Parameters.Add("@Id", SqlDbType.Int).Value = entity.Id;

                SqlAdp.DeleteCommand = SqlCmd;
                SqlAdp.DeleteCommand.ExecuteNonQuery();
                SqlCmd.Transaction.Commit();
                boolDelete = true;
                SqlCnn.Close();
            }
            catch
            {
                boolDelete = false;
                SqlCmd.Transaction.Rollback();
            }
            finally
            {
                SqlCmd.Parameters.Clear();
                SqlTrans = null;
            }
            if (SqlCmd != null)
                SqlCmd.Dispose();
            if (SqlAdp != null)
                SqlAdp.Dispose();
            return boolDelete;
        }       
    }
}
